<template>
  <div style="position: absolute; height: 100%; width: 100%;">
    <el-container>
      <el-header>
        <el-row>
          <el-col :span="3" :offset="20" style="height: 60px; line-height: 60px;">
            <el-button size="small" @click="showAddUser = true">添加用户</el-button>
          </el-col>
        </el-row>
      </el-header>
      <el-main>
        <el-table
          v-loading="listLoading"
          :data="pageInfo.records"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
        >

          <el-table-column align="center" label="用户姓名">
            <template slot-scope="scope">
              {{ scope.row.username }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="账号">
            <template slot-scope="scope">
              {{ scope.row.account }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="单位姓名">
            <template slot-scope="scope">
              {{ scope.row.institutionName }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="角色姓名">
            <template slot-scope="scope">
              {{ scope.row.roleName }}
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" width="255">
            <template slot-scope="scope">
              <el-button size="mini" @click="selectedUser=scope.row; showUpdateUser=true">修改</el-button>
              <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
              <el-button size="mini" type="primary" plain @click="resetPassword(scope.$index, scope.row)">重置密码</el-button>
            </template>
          </el-table-column>

        </el-table>
      </el-main>

      <el-footer>
        <el-pagination
          background
          :current-page="pageInfo.current"
          :page-size="pageInfo.size"
          layout="total, prev, pager, next, jumper"
          :total="pageInfo.total"
          @current-change="getUserList"
        />
      </el-footer>
    </el-container>

    <el-dialog title="添加用户" :visible.sync="showAddUser">
      <addUser v-if="showAddUser" @refresh="getUserList(1)" @closeDialog="showAddUser = false" />
    </el-dialog>

    <el-dialog title="修改用户" :visible.sync="showUpdateUser">
      <updateUser v-if="showUpdateUser" :id="selectedUser.id" @refresh="getUserList(1)" @closeDialog="showUpdateUser = false" />
    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request'
import addUser from './components/addUser.vue';
import updateUser from './components/updateUser.vue';
import qs from 'qs';

export default {
  components: { addUser, updateUser },
  data() {
    return {
      showAddUser: false,
      showUpdateUser: false,
      listLoading: true,
      pageInfo: {},
      selectedUser: null,
      searchData: {
        currentPage: 1
      }
    }
  },
  created() {
    this.getUserList(1)
  },
  methods: {
    getUserList(currentPage) {
      this.listLoading = true
      this.searchData.currentPage = currentPage;
      request({
        url: '/api/user/getUserListPage',
        method: 'get',
        params: this.searchData
      }).then((res) => {
        this.pageInfo = res.result;
        this.listLoading = false
      }).catch(() => {

      })
    },
    deleteUser(index, row) {
      this.$confirm('确认删除？')
        .then(_ => {
          request({
            url: '/api/user/deleteUser',
            method: 'post',
            data: qs.stringify({ id: row.id }),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
          }).then((res) => {
            this.getUserList(this.searchData.currentPage);
          }).catch(_ => {})
        }).catch(_ => {});
    },
    resetPassword(index, row) {
      this.$confirm('确认要重置密码吗？')
        .then(_ => {
          request({
            url: '/api/user/resetPassword',
            method: 'post',
            data: qs.stringify({ id: row.id, password: 123456 })
          }).then((res) => {
            this.$message(res.resultCodeDesc);
          }).catch(() => {})
        }).catch(_ => {});
    }
  }
}
</script>
<style scoped>
  html,body {
    height: 100%;
  }
  .el-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    text-align: center;
    width: 100%;
    position: absolute;
    top: 0;
    left:0;
  }
  .el-header {
    line-height: 60px;
    flex-grow: 0;
    flex-shrink: 0;
  }
  .el-main {
    flex-grow: 1;
    flex-shrink: 1;
    overflow-y: auto;
  }
 .el-footer {
   flex-grow: 0;
   flex-shrink: 0;
 }
</style>
